# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
import decimal
Dec = decimal.Decimal
from AdventureMoney.core import ShareProfile

class TestCase(unittest.TestCase):
	
	def setUp(self):
		self.shareList = {
			"Bob" : (ShareProfile.SHARE_EQUAL, None),
			"Eve" : (ShareProfile.SHARE_PERCENT, 90),
			"Alice" : (ShareProfile.SHARE_EXACT, 4),
		}
		
		self.shareNones = {
			"Joe" : (ShareProfile.SHARE_NONE, None),
			"Jim" : (ShareProfile.SHARE_NONE, None),
		}
		
		self.otherNames = ["Blizzard", "Monkey", "bananaphone"]
		
		self.shareA = ShareProfile.ShareProfile()
		self.shareB = ShareProfile.ShareProfile()
		self.shareEmpty = ShareProfile.ShareProfile()
		
		for name, (shareType, value) in self.shareList.iteritems():
			self.shareA.setPersonShare(name, shareType, value)
			self.shareB.setPersonShare(name, shareType, value)
			
		for name, (shareType, value) in self.shareNones.iteritems():
			self.shareB.setPersonShare(name, shareType, value)
	
	def testCmp(self):
		self.assertEqual(self.shareA, self.shareB)
		self.assertEqual(self.shareA, self.shareA)
		self.assertNotEqual(self.shareEmpty, self.shareA)
		self.assertNotEqual(self.shareA, "")
		
	def testLen(self):
		self.assertEqual(len(self.shareA), len(self.shareList))
		self.assertEqual(len(self.shareB), len(self.shareList))
		self.assertEqual(len(self.shareEmpty), 0)
		
	def testIter(self):
		for i in self.shareEmpty:
			self.fail("Item in the empty ShareProfile")
		for i in self.shareA:
			self.failUnless( (i in self.shareList), "Item in the ShareProfile is not in the original data.")
		for i in self.shareB:
			self.failIf( (i in self.shareNones), "Item from ShareProfile in also in the SHARE_NONE data.")
			
	def testContains(self):
		for i in self.shareList:
			self.failUnless( (i in self.shareB), "Item in original data is not in the ShareProfile.")
			self.failUnless( (i in self.shareA), "Item in original data is not in the ShareProfile.")
			self.failIf( (i in self.shareEmpty), "Item in original data is in the empty ShareProfile.")
		for i in self.shareNones:
			self.failIf(i in self.shareA)
			self.failIf(i in self.shareB)
			self.failIf(i in self.shareEmpty)
			
	def testCopy(self):
		copyA = self.shareA.copy()
		copyEmpty = self.shareEmpty.copy()
		self.assertEqual(self.shareA, copyA)
		self.assertEqual(self.shareEmpty, copyEmpty)
		self.assertNotEqual(self.shareA, copyEmpty)
		copyA.setPersonShare("gg", ShareProfile.SHARE_NONE)
		self.assertEqual(self.shareA, copyA)
		copyEmpty.setPersonShare("bb", ShareProfile.SHARE_EQUAL)
		self.assertNotEqual(self.shareEmpty, copyEmpty)
			
	def testSetPersonShare(self):
		for name in self.otherNames:
			for shareType in ShareProfile.ALL_SHARE_TYPES:
				self.shareA.setPersonShare(name, shareType, 100)
				
				shareType_, value_ = self.shareA.getPersonShare(name)
				self.assertEquals(shareType, shareType_)
				if shareType == ShareProfile.SHARE_NONE or shareType == ShareProfile.SHARE_EQUAL:
					self.assertEquals(None, value_)
				else:
					self.assertEquals(100, value_)
		
		for name in self.otherNames:
			self.failUnless(name in self.shareA)
			self.shareA.setPersonShare(name, ShareProfile.SHARE_NONE, None)
			self.failIf(name in self.shareA)
			
	def testGetPersonShare(self):
		for name, (shareType, value) in self.shareList.iteritems():
			for share in (self.shareA, self.shareB):
				shareType_, value_ = share.getPersonShare(name)
				self.assertEquals(shareType, shareType_)
				self.assertEquals(value, value_)
				
		for name, (shareType, value) in self.shareNones.iteritems():
			for share in (self.shareA, self.shareB, self.shareEmpty):
				shareType_, value_ = share.getPersonShare(name)
				self.assertEquals(shareType, shareType_)
				self.assertEquals(value, value_)
	
	def testCalculateShareNone(self):
		# test cases for a share profile with all SHARE_NONE types.
		noneShare = ShareProfile.ShareProfile()
		for name, (shareType, value) in self.shareNones.iteritems():
			noneShare.setPersonShare(name, shareType, value)
			
		self.assertRaises(ShareProfile.OverTotalException, noneShare.calculateShares, -1)
		try:
			noneShare.calculateShares(0)
		except Exception, e:
			self.fail(repr(e))
		self.assertRaises(ShareProfile.UnderTotalException, noneShare.calculateShares, 1)
		
	def testCalculateShareEqual(self):
		#test with all SHARE_EQUAL types.
		equalShare = ShareProfile.ShareProfile()
		numPeople = 4
		for i in range(numPeople):
			equalShare.setPersonShare(str(i), ShareProfile.SHARE_EQUAL)
			
		for i in range(100):
			shareDict = equalShare.calculateShares(i)
			values = shareDict.values()
			for j in values:
				self.assertEquals(j, Dec(i) / Dec(numPeople))
		
	
	def testCalculateSharePercent(self):
		# test with all SHARE_PERCENT types
		percentShare = ShareProfile.ShareProfile()
		# all should add up to above 100 percent
		percentList = [41, 13, 24]
		# when summed with the list above, it must equal 100
		percent100 = 22
		for i in percentList:
			percentShare.setPersonShare(str(i), ShareProfile.SHARE_PERCENT, i)
			
		self.assertRaises(ShareProfile.UnderTotalException, percentShare.calculateShares, 1)
		percentShare.setPersonShare(str(percent100), ShareProfile.SHARE_PERCENT, percent100)
		try:
			percentShare.calculateShares(1)
		except Exception, e:
			self.fail(repr(e))
		percentShare.setPersonShare("unique", ShareProfile.SHARE_PERCENT, percent100)
		self.assertRaises(ShareProfile.OverTotalException, percentShare.calculateShares, 1)
		
	def testCalculateShareExact(self):
		# test with all SHARE_EXACT types
		exactShare = ShareProfile.ShareProfile()
		exactList = ["10.50", "56.33", "9.40", "33.00", "1.99", "0"]
		total = sum(Dec(x) for x in exactList)
		
		for i in exactList:
			exactShare.setPersonShare(i, ShareProfile.SHARE_EXACT, i)
		
		self.assertRaises(ShareProfile.UnderTotalException, exactShare.calculateShares, total + 1)
		self.assertRaises(ShareProfile.OverTotalException, exactShare.calculateShares, total - 1)
		shareDict = exactShare.calculateShares(total)
		for i in exactList:
			self.assertEquals(shareDict[i], Dec(i))
		
	
	def testCalculateShares(self):
		share = ShareProfile.ShareProfile()
		exactAmount = Dec("43.21")
		share.setPersonShare("1", ShareProfile.SHARE_EXACT, exactAmount)
		share.setPersonShare("2", ShareProfile.SHARE_PERCENT, 20)
		
		self.assertRaises(ShareProfile.UnderTotalException, share.calculateShares, 100)
		self.assertRaises(ShareProfile.OverTotalException, share.calculateShares, 50)
		try:
			shareDict = share.calculateShares(Dec("54.0125"))
			self.assertEquals(shareDict["1"], Dec("43.21"))
			self.assertEquals(shareDict["2"], Dec("10.8025"))
		except Exception, e:
			self.fail(repr(e))
			
		share.setPersonShare("3", ShareProfile.SHARE_EQUAL)
		share.setPersonShare("4", ShareProfile.SHARE_EQUAL)
		
		for i in range(2):
			# now that we have equal shares, it should not fail at all
			try:
				shareDict = share.calculateShares(100)
				self.assertEquals(shareDict["1"], exactAmount)
				self.assertEquals(shareDict["2"], 20)
				self.assertEquals(shareDict["3"], Dec("18.395"))
				self.assertEquals(shareDict["4"], Dec("18.395"))
			except Exception, e:
				self.fail(repr(e))
			# adding two SHARE_NONEs shouldn't change anything the second time we go around the loop.	
			share.setPersonShare("5", ShareProfile.SHARE_NONE)
			share.setPersonShare("6", ShareProfile.SHARE_NONE)
			
		
	def testCalculatePersonShare(self):
		total = 101
		shareDict = self.shareB.calculateShares(total)
		for name in self.shareList:
			value = self.shareB.calculatePersonShare(name, total)
			self.assertEquals(shareDict[name], value)
				
		for name in self.otherNames:
			value = self.shareB.calculatePersonShare(name, total)
			self.assertEquals(0, value)
			
	def testSwapPerson(self):
		tupleOne = self.shareA.getPersonShare("Bob")
		self.shareA.swapPerson("Bob", "Jimbo")
		shareType, value = self.shareA.getPersonShare("Bob")
		self.assertEqual(shareType, ShareProfile.SHARE_NONE)
		tupleTwo = self.shareA.getPersonShare("Jimbo")
		self.assertEqual(tupleOne, tupleTwo)
		
		self.shareA.swapPerson("Jimbo", None)
		shareType, value = self.shareA.getPersonShare("Jimbo")
		self.assertEqual(shareType, ShareProfile.SHARE_NONE)
		shareType, value = self.shareA.getPersonShare("Bob")
		self.assertEqual(shareType, ShareProfile.SHARE_NONE)
	
	def testPrettyString(self):
		try:
			self.shareA.getPrettyString()
			self.shareB.getPrettyString()
			self.shareEmpty.getPrettyString()
		except Exception, e:
			self.fail(repr(e))
	
	def tearDown(self):
		pass
